﻿<?php
/**
 * Description of ManagingModule
 * 用户管理Module
 * @author
 */
class ManagingModule extends BaseModule{


    //用户ID
    private $user_id;
    //用户组ID
    private $group_id;

    public function __construct($user_id,$group_id) {
        parent::__construct();
        $this->user_id=$user_id;
        $this->group_id=$group_id;
    }

    //关键词搜索条件
    private function init($sel_user) {
        $where='';
        if (!empty($sel_user['txtaudit'])) {
            $where.= "card = '" . $sel_user['txtaudit'] . "' and ";
        }
        if (!empty($sel_user['txtusername'])) {
            $where.="username='" . $sel_user['txtusername'] . "' and ";
        }
        $where = rtrim($where, "and ");
        if(!empty($where)){
            return $where;
        }
        return 1;
    }

    /**
     * 查找所有普通用户|指定的普通用户
     */
    public function sql_user($sql ='',$type=true){
        $list=UserDao::getUserList($this->user_id,$type,$this->init($sql));
        if (!empty($list)) {
            return $list;
        }
        return false;
    }
    /**
     * 查找所有用户组|指定的用户组信息
     *
     * @param int $group_id 用户组ID
     * @return array
     */
//    public function sql_group_data() {
//        $list=UserDao::getGroupList($this->group_id);
//        foreach($list as $key=>$row){
//            $arr=NavMenuModule::get_user_menu($row['permissions']);
//            foreach ($arr as $r){
//                $list[$key]['paramArr'][]=$r['title'];
//            }
//        }
//        if (!empty($list)) {
//            return $list;
//        }
//        return false;
//    }
    /**
     * 查找所有部门|指定的部门信息
     *
     * @param int $depid 部门ID
     * @return array
     */
    public function sql_department_data($depid='') {
        $list=UserDao::getDepartmentList($depid);
        if (!empty($list)) {
            return $list;
        }
        return false;
    }

    /**
     * 添加用户组
     * @param str $group_name 用户组名
     * @param str $group_root 用户组权限
     * @param int $UserId 操作的用户ID
     */
//    public function add_group($group_name, $group_root,$username) {
//        if ($group_name && $group_root != '') {
//            $query=UserDao::sqlAddGroup($group_name, $group_root, $username);
//            return $query;
//        }
//        return false;
//    }

    /**
     * 删除 、启用 用户组
     *
     * @param int $groupid 用户组ID
     * @param int $UserId 操作人ID
     * @param int $state 用户状态
     * @return bool
     */
//    public function sql_group_delete_enable($state) {
//        if (!empty($this->group_id)) {
//          $query = UserDao::sqlDelEna($state, $this->group_id);
//           return $query;
//        }
//        return false;
//    }

    /**
     * 添加用户
     * @param array $userdata 用户信息
     * @param str;
     */
    public function auth_user_add($userdata) {
        if (is_array($userdata) && !empty($userdata)) {
            $list=UserDao::sql_Name();
            foreach($list as $row){
                $username[]=$row['username'];
            }
//            $u_list=UserDao::sqlAuditUser('',1);
//            foreach($u_list as $val){
//                $name[]=$val['username'];
//            }
            
            if(in_array($userdata['username'],$username)){
                return 5;
            }
            if(!empty($userdata['groundid'])){
                $dep=UserDao::getDepartmentList($userdata['groundid']);
                $userdata['groundname']=$dep[0]['nickname'];
            }

            if($userdata['username']){
                if(UserDao::sqlAddUser($userdata)){
                    return 1;
                }
            }
            return 4;
        }
    }
   /**
    * 添加用户|更新用户,获取用户信息转换成json格式
    * @param array $user 不是JSON格式的用户数组
    * @param int $type 该审核的操作类型
    * @param int $userid 用户ID
    */
   public function UserJson($user,$type,$userid){
       if(!empty($user)){
           $name=array("username"=>$user['username'],"nickname"=>$user['nickname']);
           $return=UserDao::sqlPendingUser($name,json_encode($user),$type,$userid);
           if($return){
               return true;
           }
           return false;
       }

   }

        /**
     * 添加部门
     * @param str $name 部门名称
     * @param str;
     */
    public function auth_dep_add($name,$username,$up_name) {
        $list=UserDao::getDepartmentList();
        foreach($list as $row){
            $depname[]=$row['nickname'];
        }
//return $depname;
if(in_array($name,$depname)){
            return 3;
        }
        if($name)
            $adduser=UserDao::sqlAddDep($name,$username,$up_name);
            return $adduser;
   }

    /**
     * 修改用户
     * @param array $userdata 用户信息
     * return bool
     */
    public function auth_user_update($userdata,$userid){
        if (is_array($userdata) && !empty($userdata)) {
            if(!empty($userdata['email'])){
             if (!preg_match("/^[0-9a-zA-Z]+(?:[\_\-][a-z0-9\-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\.[a-zA-Z]+$/i", $userdata['email'])) {
                return 2;
             }
            }
             $group=UserDao::getDepartmentList($userdata['department']);
             $groupname=$group[0]['nickname'];
                    $where = "level='".$userdata['level']."',nickname='".$userdata['nickname']."',
                    email='".$userdata['email']."',phone='".$userdata['phone']."',card='".$userdata['iccard']."',group_id='".$userdata['department']."',group_nickname='".$groupname."',is_admin='".$userdata['is_audit']."',ip_adress='".$userdata['ip_adress']."',login_land='".$userdata['login_land']."'$up_audit";
                $dao=UserDao::sqlUpUser($where,$userid);
             if($dao){
                 return 1;
             }
        }
        return 3;
    }

    /**
     * 对用户授权
     */
    public function set_user_isadmin($userdata,$userid){
        $user=UserDao::getUserList($userid,false);
        $up_audit='';
        if($user[0]['is_admin'] != $userdata['is_audit'] && $user[0]['is_admin']=='null'){
            $up_audit=',is_forbidden =0';
        }
        $where = "is_admin=".$userdata['is_audit'].",login_land='".$userdata['login_land']."'$up_audit";
        $dao=UserDao::sqlUpUser($where,$userid);
        if($dao){
            return true;
        }
        return false;
    }
    
    /**
     * 修改密码
     */
    public function update_pwd($userdata,$userid){
        if(empty($userdata['password'])) return false;
        
          $where = "password='".$userdata['password']."'";
          $dao=UserDao::sqlUpUser($where,$userid);
          if($dao){
              return true;
          }
    }
    /*
     * 删除用户
     */
    public function auth_del_enable_user(){
        $re_del=UserDao::sqlDelEnableUser($this->user_id);
       if($re_del){
           return true;
       }
        return false;
    }

    /**
     * 不经审核修改用户密码
     * @param str $pwd 用户新密码
     */
    public function auth_sql_user_pwd($oldpwd,$pwd){
        $old=md5($oldpwd.ADMIN_KEY);
        $user=UserDao::getUserList($this->user_id);
        if($old != $user[0]['password']){
            return 2;
        }else{
            $password=md5($pwd.ADMIN_KEY);
            $where = "password='".$password."'";
            $auth=UserDao::sqlUpUser($where,$this->user_id);
            if($auth){
                return 1;
            }
            return 3;
        }
    }

    /**
     * 修改管理员IP地址
     */
    public function auth_sql_ipadress($ip){
        if(!empty($ip)){
            $up_ip=UserDao::sqlUpAdminIpAdress($this->user_id,$ip);
            if($up_ip){
                return true;
            }
        }
        return false;
    }
    
    /**
     * 批量授权
     * SQL语句
     */
    public function set_piliang($sql){
        $db = DB_MySQLi::getInstance();
        $query=$db->query($sql);
        return $query;
    }
}

?>
